﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CCWin;
using YJYpublicClass;
using HVisionCH;
using System.IO;
using HalconDotNet;
using WindowsFormsApp2;
namespace WindowsFormsApp2.Froms2
{
    public partial class 双目相机 : Form
    {
        public 双目相机()
        {
            InitializeComponent();
        }


        //Form1 form1 = new Form1();
        private void 双目相机_Load(object sender, EventArgs e)
        {
            if (外设.Camera3D .ConnectionState==1)
            {
                checkConnect.Checked = true;
            }
            else
            {
                checkConnect.Checked = false ;
            }
            //ShowScreen();


        }
        private void ShowScreen(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            Screen s = Screen.AllScreens[1];
           
            this.Location = new System.Drawing.Point(s.Bounds.X, s.Bounds.Y);
            this.WindowState = FormWindowState.Maximized;
            this.Size = new Size(s.WorkingArea.Width, s.WorkingArea.Height);

            //this.Left = s.Bounds.Left + (s.Bounds.Width - this.Width) / 2;
            //this.Top = (s.Bounds.Height - this.Height) / 2;
        }



        private void 模拟PLC_Click(object sender, EventArgs e)
        {
            PublicValue.S3PLCresult = "1";
            RunLogMessage3D("发送触发信号1");
            //Form1.form1.tempoutobj
        }

        private void Photo_Click(object sender, EventArgs e)
        {
            外设.Camera3D.SendBytes("Start");
            RunLogMessage3D("触发拍照");
        }

       

        /// <summary>
        /// 在需要销毁From的时候显示调用该方法将销毁Form并释放占用内存
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        public void DisposeForm(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void ExcuImaPro_Click(object sender, EventArgs e)
        {
            DateTime time1 = DateTime.Now;
            RunLogMessage3D("触发图像处理");
            PublicFuntions.FileTimeInfo latesetfilename = PublicFuntions.GetLatestFileTimeInfo(PublicValue.RootPath3D, ".txt");
            PublicValue.CloudFilePath = latesetfilename.FileName;
            MainForm.form1 .S3Imagetest(PublicValue.CloudFilePath);

            DateTime time2 = DateTime.Now;
            string str = (time2 - time1).ToString();
            RunLogMessage3D("图像处理时间--" + str);
            RunLogMessage3D("图像处理完成");

        }

        private void Connect3D_Click(object sender, EventArgs e)
        {
            if (外设.Camera3D.ConnectionState == 0)
            {
                if (外设.Camera3D.connect())
                {
                    MessageBox.Show("相机连接成功");
                    RunLogMessage3D("相机连接成功");

                }
                else
                {

                    RunLogMessage3D("相机连接失败，请检查相机服务器是否开启");
                }
                ; 
            }
            else
            {
                MessageBox.Show("请勿重复连接相机");
                RunLogMessage3D("重复连接");
            }
        }
        //工具函数
        public  void RunLogMessage3D(string LogMessage)
        {
            string LogTime = System.DateTime.Now.ToString("HH:mm:ss");
            LogMessage = LogTime + ">>>" + LogMessage + "\r\n";
            //创建委托，用于访问主线程控件
            this.Invoke((EventHandler)delegate
            {

                log_3D.AppendText(LogMessage);
                //保持在最后一行显示
                log_3D.ScrollToCaret();
                if (log_3D.Lines.Length > 500)
                {
                    log_3D.Clear();
                }
            });
            PublicFuntions.SaveLog(PublicValue.LogPath, LogMessage);
        }

        private void 双目相机_ResizeStart(object sender, EventArgs e)
        {
            HObject h_dumpImg = null;
            HTuple h_width, h_height;
            HOperatorSet.DumpWindowImage(out h_dumpImg, this.hWindowControl1.HalconWindow);
            HOperatorSet.GetImageSize(h_dumpImg, out h_width, out h_height);
            HOperatorSet.SetPart(this.hWindowControl1.HalconWindow, 0, 0, h_height - 1, h_width - 1);
            HOperatorSet.DispObj(h_dumpImg, this.hWindowControl1.HalconWindow);
        }
    }
}
